<template>
	<div> </div>
</template>
<script>
import { dataRequest as api, automaticLogin, Logoutoflogin } from '@/api/api'
export default {
	data() {
		return {
			code: this.$route.query.code,
			path: this.$route.query.path,
		}
	},
	methods: {
		async trueloing(arr) {
			if (arr.code == 200) {
				const data = { id: 1, JWT: arr.data, userid: arr.massage, logon: false }
				if (await this.$indexedDB.getObject(data.id)) {
					// 存在修改
					this.$indexedDB.updateObject(data)
				} else {
					// 不存在 添加
					this.$indexedDB.addObject(data)
				}

				await automaticLogin()
				this.$message.success('登陆成功')
				this.$router.push(this.path)
			} else {
				this.$message.error('未绑定账户')
				this.$router.push(this.path)
			}
		},

		async login() {
			let arr = await api(['JWT', { code: this.code }])
			this.trueloing(arr)
		},
		// 增加一个判断函数
		async Bindgithub() {
			let arr = await api(['Bindgithub', { code: this.code }])
			console.log(arr)
			if (arr.code == 200) {
				this.$message.success('绑定成功')
			} else {
				this.$message.error(arr.data)
			}
			this.$router.push(this.path)
		},
	},
	mounted() {
		if (this.$route.query.code && this.$route.query.path) {
			this.code = this.$route.query.code
			this.path = this.$route.query.path
			console.log(this.code)
			if (this.path == '/user/settings') {
				// 绑定github
				this.Bindgithub()
			} else {
				this.login()
			}
		} else {
			this.$router.push('/')
		}
	},
}
</script>
